/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package TextManager;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Jaime
 */
public class FileWordStream implements Iterable<WordOccurrence>{
    
    private BufferedReader source;
    private List<WordOccurrence> words;
    private int nextWordIndex = 0;
    private CommonWordsFilter cwf;
    
    public static boolean filterCommonWords = false;
    
    public FileWordStream(File file) throws FileNotFoundException, IOException
    {
        this(new BufferedReader(new FileReader(file)));
    }
    
    public FileWordStream(BufferedReader buffer) throws IOException
    {
        if(filterCommonWords)
        {
            cwf = new CommonWordsFilter();
        }
        
        this.source = buffer;
        words = new ArrayList<WordOccurrence>();
        getWords();
        close();
        

    }
    
    /**
     * Returns next word.
     * @return Next word.
     */
    public WordOccurrence nextWord()
    {       
        
        WordOccurrence next = words.get(nextWordIndex);
        nextWordIndex++;
        
        return next;
        
    }
    
    public boolean hasNextWord()
    {
        return nextWordIndex < words.size();        
    }

    private void getWords() throws IOException {       
        
        String line;
        
        while((line = source.readLine()) != null)
        {
            if(line.equals(""))
              continue;
           
            line = WordCleaner.clean(line);
            
            StringTokenizer st = new StringTokenizer(line, " ");
            
            while(st.hasMoreElements())
            {
                WordOccurrence word = new WordOccurrence(st.nextToken());
                
                if(filterCommonWords)
                {
                    if(!cwf.isCommon(word))
                    {
                        words.add(word);
                    }
                }
                else
                {
                    words.add(word);
                }
            }
            
        }
        
    }
    
    private void close() throws IOException
    {
        source.close();
    }

    @Override
    public Iterator<WordOccurrence> iterator() {
        return new FileWordStreamIterator();
    }
    
    private class FileWordStreamIterator implements Iterator<WordOccurrence>
    {

        @Override
        public boolean hasNext() {
            return hasNextWord();
        }

        @Override
        public WordOccurrence next() {
            return nextWord();          
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException("Not supported yet.");
        }
        
    }
    
}
